<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .box {
      position: relative;
      width: 200px;
      height: 20px;
      background-color: skyblue;
      border-radius: 10px;
      line-height: 20px;
      padding: 0 1px;
      transition: all 0.1s ease;
    }

    .text {
      position: absolute;
      z-index: 999;
      width: 100%;
      color: white;
      text-align: center;
    }

    .inner {
      position: absolute;
      border-radius: 10px;
      width: 10%;
      height: 20px;
      background-color: red;
    }
  </style>
</head>

<body>
  <input type="file" name="" id="fileId" onchange="changeFn()">
  <div class="box">
    <div class="inner"></div>
    <div class="text">0%</div>
  </div>
  <script>
    function changeFn () {
      let fileDOM = document.querySelector('#fileId')
      let inner = document.querySelector('.inner')
      let text = document.querySelector('.text')
      console.log(fileDOM.files[0]); // 是这个的文件列表 FileList
      let formdata = new FormData()
      formdata.append('file', fileDOM.files[0])
      let xhr = new XMLHttpRequest()
      xhr.open('POST', 'http://localhost:3000/api/addUser')
      xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
          console.log(xhr.responseText);
        }
      }
      let percent = 0
      xhr.upload.onprogress = function (event) {
        if (event.lengthComputable) {
          percent = Math.floor(event.loaded / event.total * 100)
          console.log(percent);
          inner.style.width = percent + '%'
          text.innerText = percent + '%'
        }
      }
      xhr.send(formdata)
    }

  </script>
</body>

</html>